package com.riadeveloper.UI
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Expo;
	import com.riadeveloper.store.GetData;
	import com.riadeveloper.store.TileObj;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	public class TileHolder extends Sprite
	{
		private var status:String = "closed";
		private var curTile:Tile;
		private var overedTile:Tile = null;
		
		private var _toolTip:toolTip = new toolTip();
		private var tileAdded:Boolean= false;
		 
		private var newoveredTile:Tile = null;
		private var overedBool:Boolean = false;
		private var newTile:Tile;
		private var totalLoaded:int = 0;
		private var openTextTimer:Timer = new Timer(350,1);
		private var closeTimer:Timer = new Timer(350,1);
		private var openImgTimer:Timer = new Timer(350,1);
		public function TileHolder()
		{
			TweenMax.to(this, 0, {dropShadowFilter:{color:0x000000, alpha:.5, blurX:2, blurY:2, distance:0}});
			addEventListener(Event.ENTER_FRAME,enterFrameFn);
		}
		public function enterFrameFn(e:Event)
		{
			var _tempTile:Tile;
			if(overedBool && overedTile == newoveredTile)
			{
				
		
					showToolTip();
			
				for(var i:int = 0; i< 13;i++)
				{
					var tileObj:TileObj = new TileObj();
					tileObj =  GetData.GetValue(overedTile.name);
					
					_tempTile = getChildByName("tile"+i) as Tile;
					var value:Number = 1;
					if(overedTile.tileObj.textBoxPos =="left")
					{
						if(overedTile.mouseX > overedTile.referMc.width)
						{
							value = (1-(overedTile.mouseX-overedTile.referMc.width)/95)
						}
					}else if(overedTile.tileObj.textBoxPos =="right")
					{
						
						if((overedTile.bgMc.width-overedTile.mouseX) >overedTile.referMc.width)
						{
							
							value = 1-(-overedTile.mouseX+overedTile.bgMc.width-overedTile.referMc.width)/95;
						}
						
					}else if(overedTile.tileObj.textBoxPos =="top")
					{
						if(overedTile.mouseY > overedTile.referMc.height)
						{
							
								value = (1-(overedTile.mouseY-overedTile.referMc.height)/95);
						}
					}else 
					{
						if(overedTile.height-overedTile.mouseY >overedTile.referMc.height)
						{
								value = 1-(-overedTile.mouseY+overedTile.bgMc.height-overedTile.referMc.height)/95;
						}
						
					}
					if(value > 1) value =1;
					
					if(overedTile != _tempTile){
						_tempTile.changePosition(tileObj.changeImgArr[i],value,true);	
						_tempTile.resetImageMove();
					}else {
						_tempTile.changePosition(tileObj.changeImgArr[i],value,false);	
					}

						
					
				}
				
				overedTile.imageMove(value);
				
			}else if(overedTile != null)
			{
				
					showToolTip();
				
				for(i = 0; i< 13;i++)
				{

						_tempTile = getChildByName("tile"+i) as Tile;
						try{
							_tempTile.changePosition(null,0);
							_tempTile.resetImageMove();
						}catch(e:Error){
							
						}
					
				}
				if( overedTile.bgMc.width < overedTile.referMc.width+24)
				{					
					overedTile = newoveredTile
				}
				
			}
		
			
			
		}
			
		public function open(e:MouseEvent)
		{
			if(overedTile == null)
				overedTile = e.currentTarget as Tile;
			newoveredTile = e.currentTarget as Tile;
			overedBool = true;
		}	
	
		public function close(e:MouseEvent)
		{
			overedBool = false;
		}
		
		public function LoadedTile()
		{
				totalLoaded++;
			if(totalLoaded == 13)
			{
				for(var i:int = 0; i< 13;i++)
				{
					var _tempTile:Tile = getChildByName("tile"+i) as Tile;
					_tempTile.setEvent();;
				}	
			}
							
		}
		private function showToolTip()
		{
			var open1 = (overedTile.bgMc.width/(overedTile.referMc.width+overedTile.tileObj.changeImgArr[overedTile.id_num]["width"]));
			var open2 = (overedTile.bgMc.height/(overedTile.referMc.height+overedTile.tileObj.changeImgArr[overedTile.id_num]["height"]));
			
			var open = (open1+open2)/2;
			
			if(_toolTip.id  && overedTile.tileObj.style =="IMAGE")
			{
				var temptile:Tile = this.getChildByName(_toolTip.id) as Tile;
				
				
				if(overedTile.tileObj.textBoxPos =="right" || overedTile.id_num ==11)
				{
					_toolTip.x =	temptile.x;
					
				}else 
				{
					_toolTip.x =	temptile.x+ temptile.bgMc.width;
				}
				
				
				if(overedTile.tileObj.textBoxPos =="top")
				{
					_toolTip.y = temptile.y+temptile.bgMc.height*.8; 
					
				}else 
				{
					_toolTip.y = temptile.y+temptile.referMc.height*.8;
				}
			}
				
				
			if(open > .8  && !tileAdded && overedTile.tileObj.style =="IMAGE" )
			{
				_toolTip = new toolTip();
				_toolTip.id = overedTile.name;
				//_toolTip.descTxt.wordWrap = true;
				_toolTip.descTxt.multiline = true;
				_toolTip.descTxt.autoSize = TextFieldAutoSize.CENTER;
				_toolTip.descTxt.htmlText =overedTile.xml.content.image.activeState.highlightText;
				_toolTip.mouseEnabled = false;
				_toolTip.mouseChildren= false;
				_toolTip.bg.width = _toolTip.descTxt.width+10;
				_toolTip.maskMc.height = _toolTip.bg.height = _toolTip.descTxt.height;
				
				_toolTip.maskMc.scaleX = 0;
				TweenMax.to(_toolTip.maskMc,(5-_toolTip.bg.width/100),{scaleX:1});
				
				_toolTip.mask2Mc.scaleX = 0;
				TweenMax.to(_toolTip.mask2Mc,(5-_toolTip.bg.width/100),{scaleX:1,delay:.25});
				
				addChild(_toolTip);
				tileAdded = true;
				
			}else if(open < .8 && tileAdded)
			{
				removeChild(_toolTip)
				tileAdded = false;
			}			
									
		}
	}
}